package com.android.delivery.activity.fragment

import android.os.Handler
import androidx.recyclerview.widget.RecyclerView
import com.android.delivery.adapter.JieDanAdapter
import com.android.delivery.api.ApiService
import com.android.delivery.base.BaseListFragmet
import com.android.delivery.callback.DialogCallback
import com.android.delivery.callback.LzyResponse
import com.android.delivery.entity.Logistics
import com.android.delivery.receiver.PushDispatchListener
import com.android.delivery.receiver.PushNotifyManager
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.include_refresh_layout.*

/**
 * 待接单
 */
class DaiJieDanListFragment : BaseListFragmet<Logistics>(), PushDispatchListener {

    private val jieDanAdapter: JieDanAdapter by lazy {
        JieDanAdapter()
    }

    private var mHandle = Handler()

    companion object {
        val REFRESH_TIME = 5 * 1000L
        fun newInstance(): DaiJieDanListFragment {
            val fragment = DaiJieDanListFragment()
            return fragment
        }
    }

    override fun initData() {
        super.initData()

        PushNotifyManager.getInstance().addPushDispatchListener(this)
        beginRefreshData()
    }

    override fun getEnableRefresh(): Boolean {
        return false
    }

    private fun beginRefreshData() {
        if (mHandle != null) {
            mHandle.removeCallbacks(mRunnable)
            mHandle.postDelayed(mRunnable, REFRESH_TIME)
        }
    }

    private fun stopRefreshData() {
        if (mHandle != null) {
            mHandle.removeCallbacks(mRunnable)
        }
    }

    private val mRunnable: Runnable = object : Runnable {
        override fun run() {
            onRefresh(refreshLayout)
            mHandle.postDelayed(this, REFRESH_TIME)
        }
    }

    override fun onResume() {
        super.onResume()
        beginRefreshData()
    }

    override fun onPause() {
        super.onPause()
        stopRefreshData()
    }

    override fun onDestroy() {
        super.onDestroy()
        if (mHandle != null) {
            mHandle.removeCallbacksAndMessages(null)
        }
        PushNotifyManager.getInstance().removePushDispatchListener(this)
    }

    override fun bindRecyclerView(recLv: RecyclerView?) {
        recLv!!.adapter = jieDanAdapter
        jieDanAdapter.setOnItemClickListener(object : JieDanAdapter.OnItemClickListener {
            override fun onConfirmOrder(logistics: Logistics) {
                ApiService.pick_take(this, logistics.logisticsNo!!, object : DialogCallback<LzyResponse<Any>>() {
                    override fun onSuccess(response: Response<LzyResponse<Any>>?) {
                        onRefresh(refreshLayout)
                    }
                })
            }

            override fun onTransferOrder(logistics: Logistics) {
            }

        })

    }

    override fun loadData(page: Int, pageCount: Int) {
        ApiService.pick_wait_take(this, page, object : DialogCallback<LzyResponse<Logistics>>() {
            override fun onSuccess(response: Response<LzyResponse<Logistics>>?) {
                onGetDataSuc(response!!.body().data.records)

                finishRefreshaLayout()

            }

            override fun onError(response: Response<LzyResponse<Logistics>>?) {
                super.onError(response)

                finishRefreshaLayout()
            }
        })
    }

    /**
     * 结束下拉刷新
     */
    private fun finishRefreshaLayout() {
    }

    override fun getData(): MutableList<Logistics> {
        return jieDanAdapter.data
    }

    override fun bindData(isRefresh: Boolean, list: MutableList<Logistics>?) {
        if (isRefresh) {
            jieDanAdapter.setNewData(list)
        } else {
            jieDanAdapter.addData(list!!)
        }
    }

    fun setEnableRefresh(enableRefresh: Boolean) {
        if (refreshLayout != null) {
            refreshLayout.setEnableRefresh(false)
        }
    }

    fun onAutoRefresh() {
        onRefresh(refreshLayout)
    }

    override fun onReceOrder() {
        onAutoRefresh()
    }
}